package com.tsyun.view_project.list;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tsyun.view_project.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListViewActivity extends AppCompatActivity {
    //列表控件声明
    private ListView listView;
    //数组适配器
    private ArrayAdapter<String> dataAdapter;
    //简单适配器
    private SimpleAdapter simpleAdapter;
    private ArrayList<String> datas;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        initView();
        initData();

    }

    private void initData() {
        //初始化数组适配器
        datas = new ArrayList<>();
        datas.add("罗非鱼");
        datas.add("鱿鱼");
        datas.add("鲤鱼");
        datas.add("鲸鱼");
        //简单适配器
        dataAdapter  = new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice,datas);
        //初始化数据，并返回列表要显示的数据
        List<Map<String,String>> datas = initListData();
        String[] titles = new String[]{"data","subText"};
        int[] res = new int[]{R.id.item_data,R.id.tv_sub_text};
        simpleAdapter = new SimpleAdapter(this,datas,R.layout.item_simple,titles,res);
//        dataAdapter.notifyDataSetChanged();
        listView.setAdapter(dataAdapter);
    }

    //初始化列表数据
    private List<Map<String, String>> initListData() {
        List<Map<String,String>> datas = new ArrayList<>();
        //初始化15个数据
        for (int i = 0; i < 15; i++) {
            Map<String,String> map = new HashMap<>();
//            map.put("data","列表"+(i+1));
//            map.put("subText","子标题"+(i+1));
            datas.add(map);
        }
        return datas;
    }

    //初始化视图
    private void initView() {
        listView = findViewById(R.id.listView);

    }
}
